<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class IndexSalesman extends \app\ApiRest
{
    protected $model;
    protected $user_model;
    protected $cash_model;
    protected $wallet_model;
    protected $coach_model;
    protected $salesman_info;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\Salesman();
        $this->user_model = new \app\massage\model\User();
        $this->cash_model = new \app\massage\model\Commission();
        $this->wallet_model = new \app\massage\model\Wallet();
        $this->coach_model = new \app\massage\model\Coach();
        $this->salesman_info = $this->salesmanDataInfo();
        if(empty($this->salesman_info)) {
            $this->errorMsg("你还不是业务员");
        }
    }
    public function salesmanDataInfo()
    {
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [2, 3]];
        $data = $this->model->dataInfo($cap_dis);
        $salesman_balance = getConfigSetting($this->_uniacid, "salesman_balance");
        $data["balance"] = 0 <= $data["balance"] ? $data["balance"] : $salesman_balance;
        return $data;
    }
    public function index()
    {
        $data = $this->salesman_info;
        $user_model = new \app\massage\model\User();
        $wallet_model = new \app\massage\model\Wallet();
        $data["avatarUrl"] = $user_model->where(["id" => $this->salesman_info["user_id"]])->value("avatarUrl");
        $data["wallet_cash"] = $wallet_model->where(["coach_id" => $this->salesman_info["id"], "type" => 6])->where("status", "in", [1, 2])->sum("total_price");
        $data["order_price"] = $this->model->salesmanOrderPrice($this->salesman_info["id"]);
        $data["material_price"] = $this->model->salesmanOrderPrice($this->salesman_info["id"], 0, 3);
        $data["wallet_cash"] = round($data["wallet_cash"], 2);
        $data["salesman_channel_fx_type"] = getConfigSetting($this->_uniacid, "salesman_channel_fx_type");
        if(!empty($data["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $data["agent_name"] = $admin_model->where(["id" => $data["admin_id"], "status" => 1])->value("agent_name");
        }
        return $this->success($data);
    }
    public function salesmanChannelCash()
    {
        $channel_model = new \app\massage\model\ChannelList();
        $user_model = new \app\massage\model\User();
        $dis[] = ["salesman_id", "=", $this->salesman_info["id"]];
        $dis[] = ["status", "in", [2, 3]];
        $channel_list = $channel_model->dataList($dis, 10, "user_name,id,balance,user_id");
        $channel_balance = getConfigSetting($this->_uniacid, "channel_balance");
        if(!empty($channel_list["data"])) {
            foreach ($channel_list["data"] as &$value) {
                $value["order_price"] = $this->model->salesmanOrderPrice($this->salesman_info["id"], $value["id"]);
                $value["material_price"] = $this->model->salesmanOrderPrice($this->salesman_info["id"], $value["id"], 3);
                $value["salesman_cash"] = $this->model->getSalesmanChannelCash($this->salesman_info["id"], $value["id"]);
                $value["avatarUrl"] = $user_model->where(["id" => $value["user_id"]])->value("avatarUrl");
                $value["balance"] = 0 <= $value["balance"] ? $value["balance"] : $channel_balance;
            }
        }
        $channel_list["salesman_channel_fx_type"] = getConfigSetting($this->_uniacid, "salesman_channel_fx_type");
        return $this->success($channel_list);
    }
    public function salesmanChannelOrderList()
    {
        $input = $this->_param;
        $dis[] = ["pay_type", "=", 7];
        $dis[] = ["salesman_id", "=", $this->salesman_info["id"]];
        $dis[] = ["channel_id", "=", $input["channel_id"]];
        if(!empty($input["start_time"]) && !empty($input["end_time"])) {
            $dis[] = ["can_tx_date", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        $order_model = new \app\massage\model\Order();
        $commis_model = new \app\massage\model\Commission();
        $share_model = new \app\massage\model\CommShare();
        $data = $order_model->where($dis)->order("id desc")->field("order_code,can_tx_date,id")->paginate(10)->toArray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["can_tx_date"] = date("Y-m-d H:i:s", $v["can_tx_date"]);
                $dis = ["status" => 2, "type" => 12, "order_id" => $v["id"]];
                $v["salesman_cash"] = $commis_model->where($dis)->sum("cash");
                $v["salesman_cash"] = round($v["salesman_cash"], 2);
                $v["point_cash"] = $share_model->where(["order_id" => $v["id"], "cash_type" => 1, "comm_type" => 12])->sum("share_cash");
            }
        }
        return $this->success($data);
    }
    public function salesmanChannelOrderListV2()
    {
        $input = $this->_param;
        $dis[] = ["a.pay_type", ">", 1];
        $dis[] = ["c.type", "=", 12];
        $dis[] = ["c.status", ">", -1];
        $dis[] = ["a.salesman_id", "=", $this->salesman_info["id"]];
        if(!empty($input["channel_name"])) {
            $dis[] = ["b.user_name", "like", "%" . $input["channel_name"] . "%"];
        }
        if(!empty($input["start_time"])) {
            $dis[] = ["a.create_time", "between", $input["start_time"] . "," . $input["end_time"]];
        }
        if(isset($input["status"])) {
            $dis[] = ["a.have_tx", "=", $input["status"]];
        }
        $order_model = new \app\massage\model\Order();
        $commis_model = new \app\massage\model\Commission();
        $share_model = new \app\massage\model\CommShare();
        $channel_model = new \app\massage\model\ChannelList();
        $data = $order_model->alias("a")->join("massage_channel_list b", "a.channel_id = b.id")->join("massage_service_order_commission c", "a.id = c.order_id")->where($dis)->field("a.id,a.have_tx,a.create_time,a.order_code,a.create_time,channel_id,(a.true_service_price) as service_price,a.material_price")->group("a.id")->order("a.id desc")->paginate(10)->toarray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["status"] = $v["have_tx"];
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $dis = ["type" => 12, "order_id" => $v["id"]];
                $salesman_cash = $commis_model->where($dis)->where("status", ">", -1)->find();
                $v["salesman_cash_info"]["cash"] = !empty($salesman_cash) ? round($salesman_cash["cash"], 2) : 0;
                $v["salesman_cash_info"]["balance"] = !empty($salesman_cash) ? $salesman_cash["balance"] : 0;
                $v["salesman_cash_info"]["point_cash"] = $share_model->where(["order_id" => $v["id"], "cash_type" => 1, "comm_type" => 12])->sum("share_cash");
                $v["channel"]["channel_name"] = $channel_model->where(["id" => $v["channel_id"]])->value("user_name");
                $dis["type"] = 10;
                $v["channel"]["balance"] = $commis_model->where($dis)->where("status", ">", -1)->value("balance");
            }
        }
        return $this->success($data);
    }
    public function applyWallet()
    {
        $input = $this->_input;
        if(empty($input["apply_price"]) || $input["apply_price"] < 0) {
            $this->errorMsg("提现费最低一分");
        }
        if($this->salesman_info["cash"] < $input["apply_price"]) {
            $this->errorMsg("余额不足");
        }
        $tax_point = getConfigSetting($this->_uniacid, "tax_point");
        $balance = 100 - $tax_point;
        $key = "salesman_wallet" . $this->getUserId();
        incCache($key, 1, $this->_uniacid);
        $value = getCache($key, $this->_uniacid);
        if($value != 1) {
            delCache($key, 1, $this->_uniacid);
            $this->errorMsg("网络错误，请刷新重试");
        }
        \think\facade\Db::startTrans();
        $res = $this->model->dataUpdate(["id" => $this->salesman_info["id"]], ["cash" => $this->salesman_info["cash"] - $input["apply_price"]]);
        if($res != 1) {
            \think\facade\Db::rollback();
            delCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => $this->salesman_info["id"], "admin_id" => $this->salesman_info["admin_id"], "total_price" => $input["apply_price"], "balance" => $balance, "apply_price" => round($input["apply_price"] * $balance / 100, 2), "service_price" => round($input["apply_price"] * $tax_point / 100, 2), "code" => orderCode(), "tax_point" => $tax_point, "text" => $input["text"], "type" => 6, "last_login_type" => $this->is_app, "apply_transfer" => !empty($input["apply_transfer"]) ? $input["apply_transfer"] : 0];
        $wallet_model = new \app\massage\model\Wallet();
        $res = $wallet_model->dataAdd($insert);
        if($res != 1) {
            \think\facade\Db::rollback();
            delCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        \think\facade\Db::commit();
        decCache($key, 1, $this->_uniacid);
        return $this->success($res);
    }
    public function walletList()
    {
        $wallet_model = new \app\massage\model\Wallet();
        $input = $this->_param;
        $dis = ["coach_id" => $this->salesman_info["id"]];
        if(!empty($input["status"])) {
            $dis["status"] = $input["status"];
        }
        $dis["type"] = 6;
        $data = $wallet_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
            }
        }
        $data["extract_total_price"] = $wallet_model->capCash($this->salesman_info["id"], 2, 6);
        $data["personal_income_tax_text"] = getConfigSetting($this->_uniacid, "personal_income_tax_text");
        return $this->success($data);
    }
    public function salesmanQr()
    {
        $input = $this->_param;
        $key = "salesman" . $this->salesman_info["id"] . "-" . $this->is_app;
        $qr = getCache($key, $this->_uniacid);
        if(empty($qr)) {
            if($this->is_app == 0) {
                $input["page"] = "user/pages/channel/apply";
                $input["salesman_id"] = $this->salesman_info["id"];
                $qr = $this->user_model->orderQr($input, $this->_uniacid);
            } else {
                $page = "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/user/pages/channel/apply?salesman_id=" . $this->salesman_info["id"];
                $qr = base64ToPng(getCode($this->_uniacid, $page));
            }
            setCache($key, $qr, 86400, $this->_uniacid);
        }
        $qr = !empty($qr) ? $qr : "https://" . $_SERVER["HTTP_HOST"] . "/favicon.ico";
        return $this->success($qr);
    }
    public function unfriendChannel()
    {
        $input = $this->_input;
        $channel_model = new \app\massage\model\ChannelList();
        $channel = $channel_model->dataInfo(["id" => $input["channel_id"], "salesman_id" => $this->salesman_info["id"]]);
        if(empty($channel)) {
            $this->errorMsg("该渠道商不是你的下级");
        }
        $res = $channel_model->dataUpdate(["id" => $input["channel_id"]], ["salesman_id" => 0]);
        $qr_model = new \app\massage\model\ChannelQr();
        $qr_model->dataUpdate(["channel_id" => $input["channel_id"]], ["salesman_id" => 0]);
        return $this->success($res);
    }
    public function setSalesmanBalance()
    {
        $input = $this->_input;
        if($this->salesman_info["balance"] < $input["balance"] && getConfigSetting($this->_uniacid, "salesman_channel_fx_type") == 2) {
            $this->errorMsg("设置比例不能高于你的分成比例");
        }
        $channel_model = new \app\massage\model\ChannelList();
        $res = $channel_model->where("id", "=", $input["channel_id"])->update(["balance" => $input["balance"]]);
        return $this->success($res);
    }
    public function delSalesmanBalance()
    {
        $input = $this->_input;
        $channel_model = new \app\massage\model\ChannelList();
        $res = $channel_model->where("id", "=", $input["channel_id"])->update(["balance" => -1]);
        return $this->success($res);
    }
    public function setInvChannelBalance()
    {
        $input = $this->_input;
        $res = $this->model->dataUpdate(["id" => $this->salesman_info["id"]], ["inv_channel_balance" => $input["balance"]]);
        return $this->success($res);
    }
}

?>